﻿
//#region Ajax数据处理 by 陈航海
//提示
$.alert = function (title, msg) {
    console.log('错误提示:' + msg);
    $.messager.alert('错误提示', '请联系财务或IT!', 'error');
};

$.try = function (func) {
    try {
        if (typeof (func) == "function") {
            func();
        }
    } catch (err) { }
}

function jyDoAjaxCallByDefault(url, type, datatype, data, successCallBackFunc) {
    var _url = url;
    var returnFun = null;
    var indexLoading = null;
    var isLoading = null;

    successCallBackFunc = typeof (successCallBackFunc) != "undefined" ? successCallBackFunc : null;
    type = typeof (type) != "undefined" ? type : 'POST';
    datatype = typeof (datatype) != "undefined" ? datatype : 'json';
    data = (type.toLowerCase() == 'post') ? (typeof (data) == "string" ? data : JSON.stringify(data)) : data;
    isLoading = successCallBackFunc == undefined || successCallBackFunc == null ? false : true;

    $.ajax({
        async: successCallBackFunc == undefined || successCallBackFunc == null ? false : true, //true为异步方式
        type: type,
        url: url,
        data: data,
        cache: false,
        dataType: datatype,
        //contentType: 'application/json', //后加
        beforeSend: function (xhr) {
            //设置header
            xhr.setRequestHeader("Authorization", window.localStorage["Token"]);
            xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
            if (isLoading) {
                if (indexLoading != null && indexLoading.length > 0) {
                    try {
                        indexLoading.dialog("close");
                    } catch (e) { }
                }
                indexLoading = $.messager.progress({
                    interval: null,
                    title: '请稍等',
                    msg: '加载数据中...',
                    content: '<div class="loading"><img src="Images/loading.gif" style="width:30px;height:30px;border:0px;" /><div>数据加载中...</div></div>'
                });
                //$.messager.progress(); 

            }
        },
        complete: function () {
            $.try(function () {
                if (isLoading && indexLoading != null && indexLoading.length > 0) {
                    indexLoading.dialog("close");
                }
            });
        },
        success: function (data, textStatus) {
            try {
                if (successCallBackFunc != null && successCallBackFunc != undefined) {
                    successCallBackFunc(data);
                } else {
                    returnFun = data;
                }
            }
            catch (err) {
                $.try(function () {
                    if (isLoading && indexLoading != null && indexLoading.length > 0) {
                        indexLoading.dialog("close");
                    }
                });
                console.log("[SCRIPTS]异常[success]：_url:" + _url + ",msg:" + err);
                $.messager.alert('错误提示', '请联系财务或IT!', 'error');
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            $.try(function () {
                if (isLoading && indexLoading != null && indexLoading.length > 0) {
                    indexLoading.dialog("close");
                }
            });

            if (jqXHR.responseJSON != null) {
                var itemMsg = "";
                for (var i in jqXHR.responseJSON) {
                    itemMsg += i + ":" + jqXHR.responseJSON[i] + '\n\t';
                }
                //$.messager.alert('错误消息', '接口:\n\t' + _url + ":\n\t" + itemMsg, 'error');
                $.messager.alert('错误信息', itemMsg, 'error');
            }


        }
    });
    return returnFun;
}
//#endregion

//#region Ajax数据处理 jyDoAjaxCall
function jyDoAjaxCall(url, type, datatype, data, callBackFunction) {
    var returnFun = null;
    var indexLoading = null;
    var isLoading = null;

    type = typeof (type) != "undefined" ? type : 'post';
    datatype = typeof (datatype) != "undefined" ? datatype : 'json';
    data = (type.toLowerCase() == 'post') ? (typeof (data) == "string" ? data : JSON.stringify(data)) : data;
    callBackFunction = typeof (callBackFunction) != "undefined" ? callBackFunction : null;
    isLoading = callBackFunction == undefined || callBackFunction == null ? false : true;

    $.ajax({
        async: callBackFunction == undefined || callBackFunction == null ? false : true, //true为异步方式
        type: type,
        url: url,
        data: data,
        dataType: datatype,
        //contentType: 'application/json', //后加
        beforeSend: function (xhr) {
            //设置header
            xhr.setRequestHeader("Authorization", window.localStorage["Token"]);
            xhr.setRequestHeader("Content-Type", 'application/json;charset=utf-8');
            if (isLoading) {
                indexLoading = layer.load();
            }
        },
        complete: function () {
            if (isLoading && indexLoading != null) {
                //layer.closeLoad();
                layer.close(indexLoading);
            }
        },
        success: function (data, textStatus) {
            try {
                var jsonData = eval(data);
                if (jsonData == null || jsonData == undefined || jsonData.length < 0) {
                    alert("jsonData=null; length:" + jsonData.length);
                }
                returnFun = eval(jsonData);
                if ((callBackFunction != null && callBackFunction != undefined)
                    && (returnFun != null && returnFun != undefined)) {
                    callBackFunction(returnFun);
                } else {
                    return returnFun;
                }
            }
            catch (err) {
                if (isLoading && indexLoading != null) {
                    layer.close(indexLoading);
                }
                alert("[SCRIPTS]异常[success]：" + err);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            if (isLoading && indexLoading != null) {
                layer.close(indexLoading);
            }
            alert("[SCRIPTS]Error:\r\n1: " + jqXHR + "\r\n2: " + textStatus + "\r\n3: " + errorThrown);
        }
    });
    return returnFun;
}
//#endregion